<?php

class Exts_Models_Zones extends Exts_Db_Table_Abstract {

	protected $_name = 'loc_zones';
	protected $_primary = 'zone_id';
	protected $_rowClass = 'Zone';
	protected $_rowsetClass = 'ZoneSet';



	public function getQueryList() {
		return $this->select();
	}

	public function findById($id) {
		return $this->fetchRow(
			$this->select()->where("zone_id = ?", $id)
		);
	}

	public function getChilds($parentId) {
		$rowset = $this->fetchAll(
				$this->select()->where('parent_id=?', $parentId)
		);

		$z = array($parentId);
		if (count($rowset)) {
			foreach ($rowset as $zone) {
				$z[] = $zone->zone_id;
				$childs = $this->getChilds($zone->zone_id);
				if (count($childs)) {
					$z = array_merge($z, $childs);
				}
			}
		}
		return $z;
	}

	public function getZoneNameBySeourl($seourl) {
		$row = $this->fetchRow(
				$this->select()->where("seourl = ?", $seourl)
		);

		if (!count($row)) {
			return false;
		}
		return $row->name;
	}

	public function getZoneRowBySeourl($seourl) {
		$row = $this->fetchRow(
				$this->select()->where("seourl = ?", $seourl)
		);

		if (!count($row)) {
			return false;
		}
		return $row;
	}

	public function getZoneBySeourl($seourl) {
		$row = $this->fetchRow(
				$this->select()->where("seourl = ?", $seourl)
		);

		if (!count($row)) {
			return false;
		}
		$z = array($row->zone_id);
		$zon = $this->getChilds($row->zone_id);
		foreach ($zon as $zo) {
			if (in_array($zo, $z)) {
				continue;
			}
			$z[] = $zo;
		}
		return $z;
	}

	public function exists($name, $parentId = 0) {
		$row = $this->fetchRow(
					$this->select()
					->where('name=?', $name)
					->where('parent_id=?', $parentId)
		);

		if (count($row)) {
			return $row;
		}
		return false;
	}

	public function insertPool($strZones) {
		$zones = explode(',', $strZones);
		if (is_array($zones)) {
			$parentId = 0;
			foreach ($zones as $z) {
				$zone = $this->exists(trim($z), $parentId);
				if ($zone) {
					$parentId = $zoneId;
					$zoneId = $zone->zone_id;
					$parentId = $zoneId;
				} else {
					$zoneId = (int) $this->save(trim($z), $parentId);
					$parentId = $zoneId;
				}
			}

			return $zoneId;
		}
		return NULL;
	}

	public function save($name, $parentId = 0) {
		$row = $this->exists(trim($name), $parentId);
		if (!count($row) || $row === false ) {
			$row = $this->createRow();
		}
		
		$row->name = trim($name);
		$row->parent_id = $parentId;
		$row->seourl = $this->seourl($name);
		return $row->save();
	}

	public function getAllParentsByZoneId ( $zoneId ) {
		$zone = array();
		do {
			$row = $this->fetchRow($this->select()->where('zone_id=?', $zoneId));
			$zone[] = $row->toArray();
			$zoneId = $row->parent_id;
		} while ($zoneId != 0);
		return array_reverse( $zone );
		
	}

	public function getStrZoneById($zoneId) {
		$zone = array();
		do {
			$row = $this->fetchRow($this->select()->where('zone_id=?', $zoneId));
			$zone[] = $row->name;
			$zoneId = $row->parent_id;
		} while ($zoneId != 0);
		return implode(',', ( array_reverse($zone)));
	}

	public function isCountrie($aZone) {
		return $aZone->parent_id == 0;
	}

	public function getAllByParent( $zoneId ) {
		$query = "
			SELECT * FROM loc_zones
			WHERE zone_id in( ".implode(',',$this->getChilds( $zoneId ))." )
		";

		$rows = $this->getAdapter()->fetchAll($query);
		return $rows;
	}

	public function getSubZones($aZoneID=0) {
		/**
		 * Los paises estan definidos con parent 0, por lo que si no se especifica zona parent devuelve las zonas.
		 */
		$consulta = " SELECT *,zoneTreeLvl(zone_id) as lvl, zoneChilds(zone_id) childs FROM loc_zones WHERE parent_id = {$aZoneID} ";
		$subZones = $this->getAdapter()->fetchAll($consulta);
		return $subZones;
	}

	public function getParent($aZoneId) {
		return $this->fetchRow($this->select()->where("zone_id = (SELECT parent_id FROM loc_zones l WHERE zone_id = ?)", $aZoneId));
	}

	public function getParentLevelZones($aZoneId) {
		
		$consulta = " SELECT *, zoneChilds(zone_id) childs 
			FROM loc_zones 
			WHERE parent_id=(SELECT parent_id FROM loc_zones WHERE zone_id={$aZoneId}) ";
		$subZones = $this->getAdapter()->fetchAll($consulta);
		
		if (count($subZones) == 0) {

			return false;
		}
		return $subZones;
	}

	public function getArrayZoneByZoneId($zoneId) {
		$str = $this->getStrZoneById($zoneId);

		if (empty($str)) {
			return array();
		}
		return explode(',', $str);
	}

	/*
	 * Devuelve las zonas en un array asociativo ID => Nombre 
	 */

	public function getSubZonesArray($aZoneID=0) {
		$zones = $this->getSubZones($aZoneID);
		$zonesArray = array();
		foreach ($zones as $zone) {
			$zonesArray[$zone['zone_id']] = $zone['name'];
		}
		return $zonesArray;
	}

}

class Zone extends Exts_Db_Table_Row_Abstract {

	public function getBrothers() {
		return $this->getTable()->fetchAll(
			$this->select()->where("parent_id = {$this->parent_id}")
		);
	}

	public function getChilds() {
		return $this->getTable()->fetchAll(
			$this->select()->where("parent_id = {$this->zone_id}")
		);
	}

}

class ZoneSet extends Exts_Db_Table_Rowset_Abstract {

	public function toSelectArray() {
		$selectArray = array();
		foreach ($this as $zone) {
			$selectArray[$zone->zone_id] = $zone->name;
		}
		return $selectArray;
	}

}